package com.majsubtense.core;

import com.majsubtense.core.components.Component;
import com.majsubtense.core.components.ComponentType;
import com.majsubtense.core.events.Event;
import com.majsubtense.core.events.EventType;

import java.util.ArrayList;
import java.util.List;

public abstract class Container {
    List<Component> components = new ArrayList<>();

    public void addComponent(Container container, Component component) {
        component.setParentContainer(container);
        components.add(component);
    }

    // 还需商榷的实现方式
    public void removeComponents(Component component) {
        components.remove(component);
    }

    public <T extends Component> T getComponent(ComponentType<T> componentType) {
        ComponentType<? extends Component> currentType;

        for (Component component : components) {
            currentType = component.getComponentType();
            while (currentType != null) {
                if (currentType.equals(componentType))
                    // noinspection unchecked
                    return (T) component;
                currentType = currentType.getSuperType();
            }
        }
        return null;
    }
}
